﻿using System.Linq;
using IterationLife.FootballTeaching.Models;
using IterationLife.FootballTeaching.Repositories;
using Util;
using Util.Datas.Ef;

namespace IterationLife.Datas.Repositories.FootballProfiles {
    /// <summary>
    /// 球员位置仓储
    /// </summary>
    public class PlayerPositionRepository : RepositoryBase<PlayerPosition>, IPlayerPositionRepository {
        /// <summary>
        /// 初始化球员位置仓储
        /// </summary>
        /// <param name="unitOfWork">工作单元</param>
        public PlayerPositionRepository( IApplicationUnitOfWork unitOfWork ) : base( unitOfWork ) {
        }

        /// <summary>
        /// 获取排序号
        /// </summary>
        public int GetSortId()
        {
            return Find().Max(t => t.SortId).SafeValue() + 1;
        }

        /// <summary>
        /// 是否重复
        /// </summary>
        /// <param name="code">编码</param>
        /// <param name="name">名称</param>
        /// <param name="id">编号</param>

        public bool IsRepeat(string code, string name, string id)
        {
            var entity = Find().FirstOrDefault(t => t.Code == code || t.Name == name);
            if (entity == null)
                return false;
            var entityId = id.ToGuidOrNull();
            if (entityId.IsEmpty())
                return true;
            return entity.Id != entityId;
        }

    }
}
